import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ReferralCode, ReferralUser, Subscribe, User } from '../../model';
import { botDto } from './bot.dto';

@Injectable()
export class BotService {
    constructor(
        @InjectRepository(User)
        private userRepository: Repository<User>,
        @InjectRepository(ReferralCode)
        private refCodeRepository: Repository<ReferralCode>,
        @InjectRepository(ReferralUser)
        private refUserRepository: Repository<ReferralUser>,
        @InjectRepository(Subscribe)
        private subscribeRepository: Repository<Subscribe>
    ) {}

    async checkAndCreateUser(message: botDto) {
        try {
            const isUser = await this.userRepository.findOne({
                where: { telegramId: message.telegramId },
            });

            if (!isUser) {
                await this.userRepository.save(
                    new User({
                        telegramId: message.telegramId,
                        firstName: message.firstName,
                        lastName: message.lastName,
                        username: message.username,
                    })
                );

                if (!message.referralCode) {
                    return {
                        role: 'nobody',
                    };
                }

                const isRef = await this.refCodeRepository.findOneBy({
                    referralCode: message.referralCode,
                });

                if (!isRef) {
                    const isTrader = await this.userRepository.findOneBy({
                        nickname: message.referralCode,
                    });

                    if (isTrader) {
                        await this.subscribeRepository.save(
                            new Subscribe({
                                userTelegramId: message.telegramId,
                                traderTelegramId: isTrader.telegramId,
                            })
                        );
                    }
                } else {
                    await this.subscribeRepository.save(
                        new Subscribe({
                            userTelegramId: message.telegramId,
                            traderTelegramId: isRef.userTelegramId,
                        })
                    );
                    await this.refUserRepository.save(
                        new ReferralUser({
                            referralTelegramId: isRef.userTelegramId,
                            referralCodeId: +isRef.id,
                            userTelegramId: message.telegramId,
                        })
                    );
                }

                return {
                    role: 'nobody',
                };
            }
            if (!isUser.apiKey) {
                return {
                    role: 'nobody',
                };
            }
            if (isUser.role === 'User') {
                return {
                    result: 'user',
                };
            } else {
                return {
                    result: 'trader',
                };
            }
        } catch (err) {
            console.log(err);
            return {
                result: 'server Error',
            };
        }
    }
}
